﻿using Crap4Net;
using System;
using NUnit.Framework;
namespace Crap4Net.UnitTests.EntitiesTests
{


    [TestFixture]
    public class CombinedDataEntryTest
    {
        const string VALID_TYPE_NAME = "SomeClass";
        const string VALID_METHOD_NAME = "SomeMethod";

        private MethodSignature _Method = new MethodSignature("dll", "ns", VALID_TYPE_NAME, VALID_METHOD_NAME, new string[] { });
        private MethodSignature _AnotherMethod =
            new MethodSignature("sll", "ns", VALID_TYPE_NAME, VALID_METHOD_NAME + "a", new string[] { });

        [Test]
        public void Equals_IdenticalValues_ReturnTrue()
        {
            var target1 = new CombinedDataEntry(_Method, 1, 5);
            var target2 = new CombinedDataEntry(_Method, 1, 5);
            Assert.IsTrue(target1.Equals(target2));
        }

        [Test()]
        public void Equals_DifferentMethod_ReturnFalse()
        {
            var target1 = new CombinedDataEntry(_Method, 1, 5);
            var target2 = new CombinedDataEntry(_AnotherMethod, 1, 5);
            Assert.IsFalse(target1.Equals(target2));
        }

        [Test()]
        public void Equals_DifferentCCValue_ReturnFalse()
        {
            CombinedDataEntry target1 = new CombinedDataEntry(_Method, 5, 1);
            CombinedDataEntry target2 = new CombinedDataEntry(_Method, 5, 2);
            Assert.IsFalse(target1.Equals(target2));
        }

        [Test()]
        public void Equals_DifferentCoverageValue_ReturnFalse()
        {
            CombinedDataEntry target1 = new CombinedDataEntry(_Method, 1, 1);
            CombinedDataEntry target2 = new CombinedDataEntry(_Method, 2, 1);
            Assert.IsFalse(target1.Equals(target2));
        }

        [Test()]
        public void Equals_NullValue_ReturnFalse()
        {
            CombinedDataEntry target = new CombinedDataEntry(_Method, 1, 5);
            bool actual = target.Equals(null);
            Assert.IsFalse(actual);
        }

        [Test()]
        public void ctor_LegalValue_InitializeFields()
        {
            CombinedDataEntry target = new CombinedDataEntry(_Method, 1, 2);
            Assert.AreEqual(1, target.CoverageData);
            Assert.AreEqual(2, target.CyclomaticComplexity);
            Assert.AreEqual(_Method, target.Method);
        }

        [Test()]
        [ExpectedException(typeof(ArgumentException))]
        public void ctor_NegativeCCValue_ThrowsException()
        {
            CombinedDataEntry target = new CombinedDataEntry(_Method, 5, -1);
        }

        [Test()]
        [ExpectedException(typeof(ArgumentException))]
        public void ctor_NegativeCoverageValue_ThrowsException()
        {
            CombinedDataEntry target = new CombinedDataEntry(_Method, -1, 5);
        }

        [Test()]
        [ExpectedException(typeof(ArgumentException))]
        public void ctor_TooBigCoverageValue_ThrowsException()
        {
            CombinedDataEntry target = new CombinedDataEntry(_Method, -1, 200);
        }

        [Test]
        public void CrapLoadCalculatedCorrectly()
        {
            MethodSignature method = new MethodSignature("dll","","SomeClass", "SomeMethod",new string[]{});
            var target = new CombinedDataEntry(method, 50, 8);
            
            var actual = target.CalculateCrapLoad();

            Assert.AreEqual(8.3d, actual,0.1d);
        }

        [Test]
        public void CrapLoadCalculatedCorrectlyWhenCoverageIsZero()
        {
            MethodSignature method = new MethodSignature("dll", "", "SomeClass", "SomeMethod", new string[] { });
            CombinedDataEntry target = new CombinedDataEntry(method, 0, 5);

            var actual = target.CalculateCrapLoad();
            
            Assert.AreEqual(5.33d, actual,0.1d);
        }


    }
}
